package com.hogae.capital.controller;

import java.util.List;

import com.hogae.capital.domain.CapAccountRecord;
import com.hogae.capital.service.ICapAccountRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hogae.core.annotation.Log;
import com.hogae.core.core.controller.BaseController;
import com.hogae.core.core.domain.AjaxResult;
import com.hogae.core.enums.BusinessType;
import com.hogae.core.utils.poi.ExcelUtil;

import com.hogae.capital.domain.CapAccount;
import com.hogae.capital.service.ICapAccountService;
import com.hogae.core.core.page.TableDataInfo;

/**
 * 资金账户Controller
 *
 * @author hogae
 * @date 2021-02-26
 */
@Api(tags = "资金账户")
@RestController
@RequestMapping("/capital/capAccount")
public class CapAccountController extends BaseController {
    @Autowired
    private ICapAccountService capAccountService;

    @Autowired
    private ICapAccountRecordService capAccountRecordService;
    /**
     * 通过userId查询资金账户列表
     */
    @ApiOperation("通过userId查询资金账户列表")
    @PreAuthorize("@ss.hasPermi('capital:capAccount:list')")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public TableDataInfo list(CapAccount capAccount) {
        startPage();
        List<CapAccount> list = capAccountService.selectCapAccountList(capAccount);
        return getDataTable(list);
    }

    /**
     * 通过userId导出资金账户列表
     */
    @ApiOperation("通过userId导出资金账户列表")
    @PreAuthorize("@ss.hasPermi('capital:capAccount:export')")
    @Log(title = "资金账户", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult export(CapAccount capAccount) {
        List<CapAccount> list = capAccountService.selectCapAccountList(capAccount);
        ExcelUtil<CapAccount> util = new ExcelUtil<CapAccount>(CapAccount.class);
        return util.exportExcel(list, "capAccount");
    }

    /**
     * 通过Id获取资金账户详细信息
     */
    @ApiOperation("通过Id获取资金账户详细信息")
    @PreAuthorize("@ss.hasPermi('capital:capAccount:query')")
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(capAccountService.selectCapAccountById(id));
    }

    /**
     * 新增资金账户
     */
    @ApiOperation("新增资金账户")
    @PreAuthorize("@ss.hasPermi('capital:capAccount:add')")
    @Log(title = "资金账户", businessType = BusinessType.INSERT)
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult add(@RequestBody CapAccount capAccount) {
        return toAjax(capAccountService.insertCapAccount(capAccount));
    }

    /**
     * 修改资金账户
     */
    @ApiOperation("修改资金账户")
    @PreAuthorize("@ss.hasPermi('capital:capAccount:edit')")
    @Log(title = "资金账户", businessType = BusinessType.UPDATE)
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult edit(@RequestBody CapAccount capAccount) {
        return toAjax(capAccountService.updateCapAccount(capAccount));
    }

    /**
     * 删除资金账户
     */
    @ApiOperation("删除资金账户")
    @PreAuthorize("@ss.hasPermi('capital:capAccount:remove')")
    @Log(title = "资金账户", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(capAccountService.deleteCapAccountByIds(ids));
    }



    @Transactional
    @ApiOperation("充值资金账户")
    @PreAuthorize("@ss.hasPermi('capital:capAccount:capitalRecharge')")
    @PostMapping("/capitalRecharge")
    public  AjaxResult capitalRecharge(@RequestBody CapAccountRecord capAccountRecord){
        int i = capAccountRecordService.insertCapAccountRecord(capAccountRecord);
        if (i==1){
            CapAccount capAccount = new CapAccount();
            capAccount.setUserId(capAccountRecord.getToUserId());
            List<CapAccount> capAccounts = capAccountService.selectCapAccountList(capAccount);
            if (capAccounts.size()>0){
                capAccounts.get(0).setAmount(capAccounts.get(0).getAmount()+capAccountRecord.getAmount());
                return AjaxResult.success(capAccounts);
            }
            return AjaxResult.error();
        }

        return toAjax(i);


    }
}
